package com.jymj.service.impl;

import com.alibaba.fastjson.JSON;
import com.jymj.dao.AnalysisDao;
import com.jymj.entity.Analysis.*;
import com.jymj.entity.KeyValueEntity;
import com.jymj.entity.dto.ReportDto;
import com.jymj.entity.fhtnty.GcwtFhEntity;
import com.jymj.entity.landmanage.PjEnviorSupvsEntity;
import com.jymj.entity.landmanage.PointDjEntity;
import com.jymj.entity.landmanage.PointRecordEntity;
import com.jymj.entity.landmanage.ScoreZhenEntity;
import com.jymj.entity.vo.ComplaintsVo;
import com.jymj.service.AnalysisService;
import com.jymj.service.EnviorFileService;
import com.jymj.utils.RRException;
import com.jymj.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by ${lc} on 2021/2/3.
 */
@Service
public class AnalysisServiceImpl implements AnalysisService {
    @Autowired
    private AnalysisDao analysisDao;
    @Autowired
    private EnviorFileService enviorFileService;

    @Override
    public List<RjhjzgtjEntity> selectRjhjzgtj(String date) {
        return analysisDao.selectRjhjzgtj(date);
    }

    @Override
    public List<MonthlySummaryPoint> monthlySummaryPoint(String date) {
        List<MonthlySummaryPoint> monthlySummaryPoints = analysisDao.queryMonthlySummaryPoint(date);
        return monthlySummaryPoints;

    }


    @Override
    public List<MonthlySummaryPointRectify> monthlySummaryPointRectify(String date) {
        List<MonthlySummaryPointRectify> monthlySummaryPointRectifies = analysisDao.monthlySummaryPointRectifies(date);
        return monthlySummaryPointRectifies;
    }


    @Override
    public List<UnqualifiedCountyEntity> queryUnqualified(String date) {
        return analysisDao.queryUnqualified(date);
    }

    @Override
    public List<SpecialEntity> querySpecial(SpecialParam specialParam) {
        List<SpecialEntity> specialEntities = analysisDao.querySpecial(specialParam);
        Integer counts = 0;//问题数量
        Integer cuncounts = 0;//涉及村庄数
        for (SpecialEntity specialEntity : specialEntities) {
            specialParam.setCode(specialEntity.getCode());
            List<SpecialEntity> specialEntityList = analysisDao.querySpecialCun(specialParam);
            specialEntity.setCuncounts(specialEntityList.size());
            specialEntity.setSpecialEntities(specialEntityList);
            counts += specialEntity.getCounts();
            cuncounts += specialEntityList.size();
        }
        SpecialEntity specialEntity = new SpecialEntity();
        specialEntity.setXzqmc("合计");
        specialEntity.setCounts(counts);
        specialEntity.setCuncounts(cuncounts);
        specialEntities.add(specialEntity);
        return specialEntities;
    }

    @Override
    public List<ToiletsEntity> queryToilets(String date) {
        List<ToiletsEntity> toiletsEntities = analysisDao.queryToilets(date);
        ToiletsEntity hjtoiletsEntity1 = new ToiletsEntity();
        hjtoiletsEntity1.setZwts(0).setWzckfwts(0).setZdwsqwts(0).setWsbgjwts(0).setSsshwts(0);
        hjtoiletsEntity1.setXzqmc("总计");
        for (ToiletsEntity toiletsEntity : toiletsEntities) {
            hjtoiletsEntity1.setZwts(toiletsEntity.getZwts() == null ? 0 : toiletsEntity.getZwts() + hjtoiletsEntity1.getZwts());
            hjtoiletsEntity1.setWzckfwts(toiletsEntity.getWzckfwts() == null ? 0 : toiletsEntity.getWzckfwts() + hjtoiletsEntity1.getWzckfwts());
            hjtoiletsEntity1.setZdwsqwts(toiletsEntity.getZdwsqwts() == null ? 0 : toiletsEntity.getZdwsqwts() + hjtoiletsEntity1.getZdwsqwts());
            hjtoiletsEntity1.setWsbgjwts(toiletsEntity.getWsbgjwts() == null ? 0 : toiletsEntity.getWsbgjwts() + hjtoiletsEntity1.getWsbgjwts());
            hjtoiletsEntity1.setSsshwts(toiletsEntity.getSsshwts() == null ? 0 : toiletsEntity.getSsshwts() + hjtoiletsEntity1.getSsshwts());
        }
        toiletsEntities.add(hjtoiletsEntity1);
        return toiletsEntities;
    }

    @Override
    public List<PatrolEntity> queryCunByDate(String date) {
        return analysisDao.queryCunByDate(date);
    }

    @Override
    public List<WtDateEntity> queryDate(String date) {
        List<WtDateEntity> wtDates = analysisDao.queryDate(date);
        return wtDates;
    }

    @Override
    public List<HjzzLogEntity> queryLogByCun(String date, Integer type) {
        List<HjzzLogEntity> hjzzLogEntities = analysisDao.queryLogByCun(date, type);
        for (HjzzLogEntity hjzzLogEntity : hjzzLogEntities) {
            //查询公厕问题
            List<PjEnviorSupvsEntity> pjEnviorSupvsEntities = analysisDao.queryGcwt(date, hjzzLogEntity.getCode(), type);
            Map<Integer, List<PjEnviorSupvsEntity>> map = pjEnviorSupvsEntities.stream().collect(Collectors.groupingBy(
                    PjEnviorSupvsEntity::getGddwid, LinkedHashMap::new, Collectors.toList()
            ));
            Object[] arr = map.keySet().toArray();
            List<GcwtFhEntity> gcwtFhEntities = new ArrayList<>();
            for (Object array : arr) {
                GcwtFhEntity gcwtFhEntity = new GcwtFhEntity();
                String proj = array + "";
                gcwtFhEntity.setBh(analysisDao.queryGcbh(Integer.valueOf(proj)));
                gcwtFhEntity.setId(Integer.valueOf(proj));
                gcwtFhEntity.setPjEnviorSupvsEntities(map.get(Integer.valueOf(proj)) == null ?
                        new ArrayList<>() : map.get(Integer.valueOf(proj)));
                gcwtFhEntities.add(gcwtFhEntity);
            }
            hjzzLogEntity.setGcwtFhEntitys(gcwtFhEntities);
            //查询村内数据
            List<ProblemFlEntity> problemFlEntities = analysisDao.queryCn(date, hjzzLogEntity.getCode(), 1, type);
            hjzzLogEntity.setProblemFlEntities(problemFlEntities);
            List<ProblemFlEntity> problemFlEntities1 = analysisDao.queryCn(date, hjzzLogEntity.getCode(), 2, type);
            hjzzLogEntity.setProblemFlEntitieList(problemFlEntities1);
        }
        return hjzzLogEntities;
    }

    @Override
    public List<TownProblemCountEntity> queryTownProblem() {
        return analysisDao.queryTownProblem();
    }

    @Override
    public List<WttzEntity> queryTz(String date, Integer type) {
        List<WttzEntity> wttzEntities = analysisDao.queryTz(date, type);
        Map<String, List<WttzEntity>> map = wttzEntities.stream().collect(Collectors.groupingBy(
                WttzEntity::getCode, LinkedHashMap::new, Collectors.toList()));
        Object[] array = map.keySet().toArray();
        List<WttzEntity> wttzEntities1 = new ArrayList<>();
        for (Object array1 : array) {
            WttzEntity wttzEntity = new WttzEntity();
            String proj = array1 + "";
            wttzEntity.setXzqmc(proj);
            wttzEntity.setZhen(map.get(proj) == null ? "" : map.get(proj).get(0).getZhen());
            wttzEntity.setXzqmc(map.get(proj) == null ? "" : map.get(proj).get(0).getXzqmc());
            wttzEntity.setCounts(map.get(proj) == null ? 0 : map.get(proj).size());
            wttzEntity.setWttzEntities(map.get(proj) == null ? new ArrayList<>() : map.get(proj));
            wttzEntity.setCzlx(map.get(proj) == null ? "" : map.get(proj).get(0).getCzlx());
            wttzEntity.setCzdl(map.get(proj) == null ? "" : map.get(proj).get(0).getCzdl());
            wttzEntities1.add(wttzEntity);
        }
        return wttzEntities1;
    }

    @Override
    public List<TownProblemCountEntity> queryWzg(ReportDto reportDto) {
        return analysisDao.queryWzg(reportDto);
    }

    @Override
    public void saveGzwt(OnAccountEntity onAccountEntity) {
        analysisDao.saveGzwt(onAccountEntity);
    }

    @Override
    public List<OnAccountDeductionEntity> getOnAccountDeduction(String date) {
        if (date == null || date.isEmpty()) {
            date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        }
        List<OnAccountDeductionEntity> onAccountDeductionEntities = analysisDao.getOnAccountDeduction(date);
        return onAccountDeductionEntities;
    }

    @Override
    public List<? extends Object> getOnAccountByDate(String date, Integer type) {
        if (type == null) {
            throw new RRException("'type' 参数不能为空");
        }
        OnAccountEntity onAccountByDate = analysisDao.getOnAccountByDate(date, type);
        if (onAccountByDate == null){
            return null;
        }
        if (type == 2) {
            return JSON.parseArray(onAccountByDate.getJsontext(), ScoreZhenEntity.class);
        } else if (type == 3) {
            return JSON.parseArray(onAccountByDate.getJsontext(), RjhjzgtjEntity.class);
        } else if (type == 4){
            return JSON.parseArray(onAccountByDate.getJsontext(), ComplaintsVo.class);
        }
        return null;
    }

    @Override
    public List<HjzzLogEntity> getDayLogByCun(String date, Integer type) {
        List<HjzzLogEntity> hjzzLogEntities = analysisDao.getLogByCun(date, type);
        for (HjzzLogEntity hjzzLogEntity : hjzzLogEntities) {
            //查询公厕问题
            List<PjEnviorSupvsEntity> pjEnviorSupvsEntities = analysisDao.getGcwt(date, hjzzLogEntity.getCode(), type);
            Map<Integer, List<PjEnviorSupvsEntity>> map = pjEnviorSupvsEntities.stream().collect(Collectors.groupingBy(
                    PjEnviorSupvsEntity::getGddwid, LinkedHashMap::new, Collectors.toList()
            ));
            Object[] arr = map.keySet().toArray();
            List<GcwtFhEntity> gcwtFhEntities = new ArrayList<>();
            for (Object array : arr) {
                GcwtFhEntity gcwtFhEntity = new GcwtFhEntity();
                String proj = array + "";
                gcwtFhEntity.setBh(analysisDao.queryGcbh(Integer.valueOf(proj)));
                gcwtFhEntity.setId(Integer.valueOf(proj));
                gcwtFhEntity.setPjEnviorSupvsEntities(map.get(Integer.valueOf(proj)) == null ?
                        new ArrayList<>() : map.get(Integer.valueOf(proj)));
                gcwtFhEntities.add(gcwtFhEntity);
            }
            hjzzLogEntity.setGcwtFhEntitys(gcwtFhEntities);
            //查询村内数据
            //如果是空返回一个空对象
            List<DayProblemEntity> dayProblemEntities = new ArrayList<>();
            dayProblemEntities.add(new DayProblemEntity());
            List<DayProblemEntity> dayProblemEntitites = analysisDao.getCn(date, hjzzLogEntity.getCode(), 1, type);
            hjzzLogEntity.setDayProblemEntities(dayProblemEntitites.get(0) == null ? dayProblemEntities : dayProblemEntitites);
            List<DayProblemEntity> dayProblemEntitites1 = analysisDao.getCn(date, hjzzLogEntity.getCode(), 2, type);
            hjzzLogEntity.setDayProblemEntitielist(dayProblemEntitites1.get(0) == null ? dayProblemEntities : dayProblemEntitites1);
        }
        return hjzzLogEntities;
    }

    @Override
    public List<PointRecordEntity> getDjByMon(String date) {
        List<PointRecordEntity> pointRecordEntities = analysisDao.getDjByMon();
        for (PointRecordEntity pointRecordEntity : pointRecordEntities) {
            pointRecordEntity.setPointDjEntities(analysisDao.queryDjtj(date, pointRecordEntity.getId()));
        }
        return pointRecordEntities;
    }

    @Override
    public List<WzgqkEntity> getWzgqk(String date) {
        List<WzgqkEntity> wzgqk = analysisDao.getWzgqk(date);
        for (WzgqkEntity wzgqkEntity : wzgqk) {
            List<KeyValueEntity> keyValueEntities = analysisDao.getWzgqkcun(date,wzgqkEntity.getCode());
            wzgqkEntity.setKeyValueEntities(keyValueEntities);
            wzgqkEntity.setCuncount(keyValueEntities.size());
        }
        return wzgqk;
    }
}
